package com.linwen.tips.generics;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GenericsTest {

    private static void getAndUpdateBalanceResultMap(String key, Map<String, List<TransferBalanceDTO>> compareResultListMap,
                                              List<TransferBalanceDTO> balanceDTOs) {
        // getOrDefault 如果存在key，则拿出来   否则返回默认值
        List<TransferBalanceDTO> tempList = compareResultListMap.getOrDefault(key, new ArrayList<>());
        tempList.addAll(balanceDTOs);
        compareResultListMap.put(key, tempList);
    }

    private static void getAndUpdateDetailResultMap(String key, Map<String, List<TransferDetailDTO>> compareResultListMap,
                                             List<TransferDetailDTO> detailDTOS) {
        List<TransferDetailDTO> tempList = compareResultListMap.getOrDefault(key, new ArrayList<>());
        tempList.addAll(detailDTOS);
        compareResultListMap.put(key, tempList);
    }

    private static <T> void getAndUpdateResultMapOptimize(String key, Map<String, List<T>> compareResultListMap, List<T> accountingDTOS) {
        List<T> tempList = compareResultListMap.getOrDefault(key, new ArrayList<>());
        tempList.addAll(accountingDTOS);
        compareResultListMap.put(key, tempList);
    }

    public static void main(String[] args) {
        String key = "key1";
        Map<String, List<TransferBalanceDTO>> objectObjectHashMap = new HashMap<>();
        List<TransferBalanceDTO> value1 = new ArrayList<>();
        value1.add(TransferBalanceDTO.builder().name("value1").build());
        List<TransferBalanceDTO> value2 = new ArrayList<>();
        value2.add(TransferBalanceDTO.builder().name("value2").build());
        objectObjectHashMap.put("key1",value1);
        objectObjectHashMap.put("key2",value2);
        List<TransferBalanceDTO> balanceDTOs = new ArrayList<>();
        balanceDTOs.add(TransferBalanceDTO.builder().name("value3").build());
        getAndUpdateBalanceResultMap(key,objectObjectHashMap,balanceDTOs);
        System.out.println(balanceDTOs);
        System.out.println(objectObjectHashMap);
    }
}
